package com.remist.web.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.remist.common.Constant;

/**
 * Servlet implementation class InitServlet
 */
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitServlet() {
        super();
    }

    private void initProperties(){
    	 Properties prop = new Properties();
    	 String path= InitServlet.class.getResource("/").getPath();
    	 String filePath = path.substring(0, path.length()- "classes/".length()) +"parameter.properties" ;
    	 filePath = filePath.replace("\\", "/");
    	 System.out.println( "filePath="+filePath );
        
    }
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
//		initProperties();
		String path = config.getServletContext().getRealPath("/")+"WEB-INF"+File.separator;
//		System.out.println (  "path="+path );
		path = path.replace("\\", "/");
		Properties prop = new Properties();
		try {
			String filePath =path  +"parameter.properties" ;
			prop.load(new FileInputStream(filePath)); 
			Constant.setIS_SHOW_VALIDATE_CODE( Boolean.parseBoolean(prop.getProperty("IS_SHOW_VALIDATE_CODE")));
		 } catch (IOException e) {
		     e.printStackTrace();
		 } 
		Constant.setWEB_ROOT_PATH(path);
		ApplicationContext springCtx=WebApplicationContextUtils.getWebApplicationContext( config.getServletContext() );
		Constant.setSpringCtx(springCtx);
	}

}
